<template>
  <div class="app-container flex-cloumn">

    <el-form :model="queryEngineConfigParams" ref="queryRef" v-show="showSearch"
             @submit.prevent='search'>
      <el-row :gutter="12">
        <el-col :span="4">
          <el-form-item label="组件名称" prop="ppName">
            <el-input
                v-model="queryEngineConfigParams.ppName"
                placeholder="请输入组件名称"
                clearable
                :style="{width: '100%'}"
                @keyup.enter="handleConfigQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="1.5">
          <el-form-item>
            <el-button type="primary" icon="Search" size="small"
                       style="margin: 3px 3px"
                       v-hasPermi="['pricingconfig:engineconfig:list']"
                       @click="handleConfigQuery">搜索
            </el-button>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
    <el-row :gutter="12" style="margin: 0px 1px">
      <el-button
          type="primary"
          icon="Plus"
          style="margin: 3px 3px"
          size="small"
          @click="handleConfigAdd"
          v-hasPermi="['pricingconfig:engineconfig:add']"
      >新增
      </el-button>

      <el-button
          type="success"
          icon="Edit"
          size="small"
          style="margin: 3px 3px"
          :disabled="single"
          @click="handleConfigUpdate"
          v-hasPermi="['pricingconfig:engineconfig:edit']"
      >修改
      </el-button>
      <el-button
          type="danger"
          style="margin: 3px 3px"
          icon="Delete"
          size="small"
          :disabled="multiple"
          @click="handleConfigDelete"
          v-hasPermi="['pricingconfig:engineconfig:remove']"
      >删除
      </el-button>
    </el-row>
    <xTable ref="refConfigTable" refId="refConfigTable" multiple="false" v-loading="loading"
            :data="configList" class="tableDragger_form"
            @selection-change="handleConfigSelectionChange"
            highlight-current-row>
      <template #columList>
        <el-table-column label="序号" type="index" width="50"/>
        <el-table-column label="组件代码" align="left" width="130" prop="ppCode" :show-overflow-tooltip="true"/>
        <el-table-column label="组件名称" align="left" width="130" prop="ppName" :show-overflow-tooltip="true"/>
        <el-table-column label="模块名称" align="left" width="150" prop="dllName" :show-overflow-tooltip="true"/>

        <el-table-column label="市场计算函数入口" align="left" width="160" prop="dllEntry" :show-overflow-tooltip="true"/>
        <el-table-column label="现金流函数入口" align="left" width="160" prop="dllEntryCf" :show-overflow-tooltip="true"/>
        <el-table-column label="事件函数入口" align="left" width="160" prop="dllEntryEvent"
                         :show-overflow-tooltip="true"/>

        <el-table-column label="模型类型" align="left" prop="method" width="160" :show-overflow-tooltip="true">
        </el-table-column>

        <el-table-column label="支持的产品类型" align="left" prop="products" :show-overflow-tooltip="true"/>
      </template>
    </xTable>

    <pagination
        v-show="configTotal>0"
        :total="configTotal"
        v-model:page="queryEngineConfigParams.pageNum"
        v-model:limit="queryEngineConfigParams.pageSize"
        @pagination="getEngineConfigList"
    />


    <!-- 添加或修改 引起参数 对话框 -->
    <el-dialog :title="configTitle" v-model="configOpen" width="480px" :close-on-click-modal="false" append-to-body>
      <el-form ref="configRef" :model="configForm" :rules="configRules" label-width="120px">
        <el-form-item label="组件代码" prop="ppCode">
          <el-input v-model="configForm.ppCode" :style="{width: '100%'}" maxlength="50"
                    :disabled="configDisabled.ppCodeDisabled"></el-input>
        </el-form-item>
        <el-form-item label="组件名称" prop="ppName">
          <el-input v-model="configForm.ppName" :style="{width: '100%'}" maxlength="100"
                    :disabled="configDisabled.ppNameDisabled"></el-input>
        </el-form-item>
        <el-form-item label="DLL名称" prop="dllName">
          <el-input v-model="configForm.dllName" :style="{width: '100%'}" maxlength="50"
                    :disabled="configDisabled.dllNameDisabled"></el-input>
        </el-form-item>
        <el-form-item label="市场计算函数入口" prop="dllEntry">
          <el-input v-model="configForm.dllEntry" :style="{width: '100%'}" maxlength="50"
                    :disabled="configDisabled.dllEntryDisabled"></el-input>
        </el-form-item>
        <el-form-item label="现金流函数入口" prop="dllEntryCf">
          <el-input v-model="configForm.dllEntryCf" :style="{width: '100%'}" maxlength="50"
                    :disabled="configDisabled.dllEntryCfDisabled"></el-input>
        </el-form-item>
        <el-form-item label="事件函数入口" prop="dllEntryEvent">
          <el-input v-model="configForm.dllEntryEvent" :style="{width: '100%'}" maxlength="50"
                    :disabled="configDisabled.dllEntryCfDisabled"></el-input>
        </el-form-item>

        <el-form-item label="模型类型" prop="method">
          <el-select
              v-model="configForm.method"
              placeholder="模型类型"
              clearable
              size="small"
              :style="{width: '100%'}"
          >
            <el-option
                v-for="dict in calcMethodType"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="支持的产品类型" prop="products">
          <el-input v-model="configForm.products" :style="{width: '100%'}" type="textarea" maxlength="200"
                    :disabled="configDisabled.products"></el-input>
        </el-form-item>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitConfigForm">确 定</el-button>
          <el-button @click="cancelConfig">取 消</el-button>
        </div>
      </template>

    </el-dialog>

  </div>
</template>

<script setup name="Engineconfig">

import {ref} from "vue";

import number from "@/xquant/base/number";


import {
  queryEngineConfigs,
  saveConfig,
  updateConfig,
  deleteConfig,
  queryContractCfgCountByPPCode
} from "@/api/pricingconfig/engineconfig";
import { isVoid } from "../../../utils/util";

const {proxy} = getCurrentInstance();
const {calcMethodType} = proxy.useDict("calcMethodType");
// 引擎列表
const configList = ref([]);
const selectionConfigRow = ref([]);
const configOpen = ref(false);
const configTitle = ref('');

const showSearch = ref(true);

const loading = ref(true);
//索引
const indexs = ref([]);
//数据
const rows = ref([]);

const single = ref(true);
const multiple = ref(true);
const configTotal = ref(0);
const action = ref('');

const data = reactive({
  configForm: {
    ppCode: undefined,   //组件代码
    ppName: undefined,   //组件名称
    dllName: undefined,  //DLL名称
    dllEntry: undefined, //市场计算函数入口
    dllEntryCf: undefined, //现金流函数入口
    dllEntryEvent: undefined, //事件函数入口
    method: undefined,        //模型类型
    products: undefined,       //支持的产品类型
  },
  configDisabled: {
    ppCodeDisabled: false,
    ppNameDisabled: false,
    dllNameDisabled: false,
    dllEntryDisabled: false,
    dllEntryCfDisabled: false,
    dllEntryEventDisabled: false,
    methodDisabled: false,
    productsDisabled: false
  },
  queryEngineConfigParams: {
    pageNum: 1,
    pageSize: 10,
    ppName: undefined
  },
  configRules: {
    ppCode: [{required: true, message: "组件代码不能为空", trigger: "blur"}],
    ppName: [{required: true, message: "组件名称不能为空", trigger: "blur"}],
    dllName: [{required: true, message: "DLL名称不能为空", trigger: "blur"}],
    dllEntry: [{required: true, message: "市场计算函数入口不能为空", trigger: "blur"}],
    dllEntryCf: [{required: false, message: "现金流函数入口不能为空", trigger: "blur"}],
    dllEntryEvent: [{required: false, message: "事件函数入口不能为空", trigger: "blur"}],
    method: [{required: true, message: "模型类型不能为空", trigger: "blur"}],
    products: [{required: false, message: "产品类型不能为空", trigger: "blur"}],
  }
});
const {
  configForm,
  configRules,
  configDisabled,
  queryEngineConfigParams
} = toRefs(data);


/** 查询引擎配置列表列表 */
function getEngineConfigList() {
  loading.value = true;
  queryEngineConfigs(queryEngineConfigParams.value).then(response => {
    configList.value = response.list;
    configTotal.value = response.total;
    loading.value = false;
  });
}

function handleConfigQuery() {
  queryEngineConfigParams.value.pageNum = 1;
  getEngineConfigList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleConfigQuery();
}


/**引擎配置 新增按钮操作 */
function handleConfigAdd() {
  resetConfig();
  configOpen.value = true;
  configTitle.value = "新增引擎配置";
  configDisabled.value.ppCodeDisabled = false;
  action.value = 'add';
}

/**
 *
 * 引擎配置 修改按钮操作
 * */
function handleConfigUpdate() {
  action.value = 'update';
  configOpen.value = true;
  configTitle.value = "修改引擎配置";

  resetConfig();
  let data = rows.value[0];
  configDisabled.value.ppCodeDisabled = true;
  configForm.value.ppCode = data.ppCode;
  configForm.value.ppName = data.ppName;
  configForm.value.dllName = data.dllName;
  configForm.value.dllEntry = data.dllEntry;
  configForm.value.dllEntryCf = data.dllEntryCf;
  configForm.value.dllEntryEvent = data.dllEntryEvent;
  configForm.value.method = data.method;
  configForm.value.products = data.products;
}

/**
 *
 * 修改/新增   保存操作
 * */
function submitConfigForm() {
  proxy.$refs.configRef.validate(valid => {
    if (valid) {
      var param = {};
      param.ppCode = configForm.value.ppCode;
      param.ppName = configForm.value.ppName;
      param.dllName = configForm.value.dllName;
      param.dllEntry = configForm.value.dllEntry;
      param.dllEntryCf = configForm.value.dllEntryCf;
      param.dllEntryEvent = configForm.value.dllEntryEvent;
      param.method = configForm.value.method;
      param.products = configForm.value.products;
      if(param.products == undefined || param.products == null){
        param.products = '';
      }

      if (action.value == 'update') {
        updateConfig(param).then(response => {
          if (response.success) {
            proxy.$modal.msgSuccess("修改成功");
            configOpen.value = false;
            getEngineConfigList();
          } else {
            proxy.$modal.msgError(response.msg);
          }
        });
      } else {
        param.dllEntryType = 1;
        saveConfig(param).then(response => {
          if (response.success) {
            proxy.$modal.msgSuccess("新增成功");
            configOpen.value = false;
            getEngineConfigList();
          } else {
            proxy.$modal.msgError(response.msg);
          }
        });
      }
    }
  });
}

/** 引擎配置 删除按钮操作 */
function handleConfigDelete() {
  var params = {
    ppCode: rows.value[0].ppCode
  };
  queryContractCfgCountByPPCode(params).then(response => {
    if (response > 0) {
      proxy.$modal.alertError("该计算组件有关联合约，请检查!");
    } else {
      deleteEngineConfig();
    }
  });
}


function deleteEngineConfig() {
  var ppCodes = [];
  rows.value.forEach(function (item) {
    ppCodes.push(item.ppCode);
  })
  var params = ppCodes.join(',');
  proxy.$modal.confirm('是否确认删除此配置？').then(function () {
    return deleteConfig(params);
  }).then(() => {
    getEngineConfigList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}


/**
 *
 *  多选框选中数据
 * */
function handleConfigSelectionChange(selection) {

  if (selection.length == 0) {
    indexs.value = [];
  } else {
    indexs.value = selection.map(item => item.index);
    rows.value = selection.map(item => item);
  }
  single.value = selection.length != 1;
  multiple.value = !selection.length;
  if (selection.length == 1) {
    return;
  }
  if (selection.length != 1) {

  }
}

/** 取消按钮 */
function cancelConfig() {
  configOpen.value = false;
  resetConfig();
}

/** 表单重置 */
function resetConfig() {
  configForm.value = {
    ppCode: undefined,   //组件代码
    ppName: undefined,   //组件名称
    dllName: undefined,  //DLL名称
    dllEntry: undefined, //市场计算函数入口
    dllEntryCf: undefined, //现金流函数入口
    dllEntryEvent: undefined, //事件函数入口
    method: undefined,        //模型类型
    products: undefined       //支持的产品类型
  };
  proxy.resetForm("configRef");
}

function rowClick(row, column, event) {
  let refsElTable = proxy.$refs.refConfigTable; // 获取表格对象
  let findRow = selectionConfigRow.value.find(c => c.rowIndex == row.rowIndex);
  if (findRow) {
    refsElTable.toggleRowSelection(row, false);
    return;
  }
  refsElTable.clearSelection();
  refsElTable.toggleRowSelection(row); // 调用选中行方法
};

function rowStyle({row, rowIndex}) {
  Object.defineProperty(row, 'rowIndex', { //给每一行添加不可枚举属性rowIndex来标识当前行
    value: rowIndex,
    writable: true,
    enumerable: false
  })
};

function rowClassName({row, rowIndex}) {
  //把每一行的索引放进row
  row.index = rowIndex;
  let rowName = "",
      findRow = selectionConfigRow.value.find(c => c.rowIndex === row.rowIndex);
  if (findRow) {
    rowName = "current-row"; // elementUI 默认高亮行的class类 不用再样式了^-^,也可通过css覆盖改变背景颜色
  }
  return rowName; //也可以再加上其他类名 如果有需求的话
};

getEngineConfigList();

</script>
